package ipebg.mzk.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.util.Properties;


/**
 * 
 * @author H2601967
 *
 */
public class Tools {
	// ----------计算总页数----------
		public static int getTotalPage(int total,int countPerPage){
			int totalCount = 0;
			if (total > 0) {
				if (total <= countPerPage) {
					totalCount = 1;
				}
				if (total > countPerPage) {
					if (total % countPerPage != 0) {
						totalCount = total / countPerPage + 1;
					}
					if (total % countPerPage == 0) {
						totalCount = total / countPerPage;
					}
				}
			} else {
				totalCount = 1;
			}
			return totalCount;
		}		
	// 获取输入字符串的MD5
	public static String getMD5(String input) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] messageDigest = md.digest(input.getBytes());
			BigInteger number = new BigInteger(1, messageDigest);
			String hashtext = number.toString(16);
			// Now we need to zero pad it if you actually want the full 32
			// chars.
			while (hashtext.length() < 32) {
				hashtext = "0" + hashtext;
			}
			return hashtext;
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}
	}

	// --------使用jdbc获取数据库连接------
	public Connection reConn() {
		InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("init.properties");
		Properties prop = new Properties();
		try {
			prop.load(inStream);
		} catch (IOException e) {
			e.printStackTrace();
		}

		Connection conn = null;
		try {
			Class.forName(prop.getProperty("datasource.driverClassName"));
			conn = DriverManager.getConnection(
					prop.getProperty("datasource.url"),
					prop.getProperty("datasource.username"),
					prop.getProperty("datasource.password"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	// 获取 MySql Sequence
	public static String getSeq(String sname) {
		return new Tools().getSequence(sname);
	}

	public String getSequence(String sname) {
		Connection conn = this.reConn();
		int sid = 0;
		String result = "";
		try {
			Statement stmt = conn.createStatement();
			String sql = "select "+sname+".nextval from dual";
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				sid = rs.getInt("nextval");
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (sname.equals("HTMLID_SEQ")) {
			result = new DecimalFormat("html000000").format(sid);
		} else if(sname.equals("PIID_SEQUENCE")) {
			result = new DecimalFormat("piId00000000").format(sid);
		}  else if(sname.equals("SIGN_LINE_ID")) {
			result = new DecimalFormat("SLI00000000").format(sid);
		}  else if(sname.equals("USER_ID")) {
			result = new DecimalFormat("UUI00000000").format(sid);
		}  else if(sname.equals("FORM_ID_SEQ")){
			result = new DecimalFormat("FORM000000").format(sid);
		}  else if(sname.equals("C_DYNAMIC_FIELDS_ID_SEQ")){
			result = new DecimalFormat("field000000").format(sid);
		}  else if(sname.equals("R_DYNAMIC_FORM_VALUES_ID_SEQ")){
			result = new DecimalFormat("FV000000000000000000").format(sid);
		}
		else {
			result = sid + "";
		}
		return result;
	}

	public static void main(String[] args) {
		System.out.println(Tools.getSeq("HTMLID_SEQ"));
	}
}
